package com.ymk.cmd;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.DateUtil;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.member.MemberBO;
import com.ymk.bo.user.PayPaymentBO;
import com.ymk.bo.user.UserInfoBO;
import com.ymk.bo.work.WorkEntryBO;
import com.ymk.core.code.cmd.MemberCmdCode;
import com.ymk.core.code.cmd.UserCmdCode;
import com.ymk.core.code.cmd.WorkEntryCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.core.constants.WxConstants;
import com.ymk.service.IWorkEntryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Service
public class WorkEntryPatchCmd extends ACmd {
    private final static Logger logger = LoggerFactory.getLogger(WorkEntryPatchCmd.class);

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        WorkEntryBO reqData = super.getData(sign, WorkEntryBO.class);

        this.WorkEntryService.update(reqData);

        //查询入职信息
//        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
//                .addParam("id",reqData.getId());
//        WorkEntryBO workEntryBO = this.WorkEntryService.get(paramsBuilder.build());
//
//        // 查询用户信息
//        paramsBuilder.clearParam().addParam("userId",workEntryBO.getUserId());
//        CmdSign userGetCmd = CmdSign.Builder.newBuild(sign)
//                .setCmdCode(UserCmdCode.USER_GET)
//                .setSource(paramsBuilder.build())
//                .setScope(SysConstants.CMD_SERVER_SCOPE)
//                .build();
//        UserInfoBO userInfoBO = this.scheduler.schedule(request, userGetCmd,UserInfoBO.class);
//
//
//        //申请通过 给用户推送消息
//        if (reqData.getIsEntry() == 1){
//            sendSucessMessage(userInfoBO,workEntryBO);
//
//        }

        return respSign;
    }

    public void sendSucessMessage(UserInfoBO userInfoBO,  WorkEntryBO workEntryBO){

        String message = "岗位名称："+workEntryBO.getWorkName()+"。岗位补贴："+workEntryBO.getSubsidyCondition()+",补贴金额："+workEntryBO.getSubsidyAmount()+"元。";

            ParamsBuilder name1 = ParamsBuilder.newBuild()
                    .addParam("value",userInfoBO.getRealName());
            ParamsBuilder thing2 = ParamsBuilder.newBuild()
                    .addParam("value", message);
            ParamsBuilder thing3 = ParamsBuilder.newBuild()
                    .addParam("value","入职申请通过");

            ParamsBuilder data = ParamsBuilder.newBuild()
                    .addParam("name1",name1.build())
                    .addParam("thing2",thing2.build())
                    .addParam("thing3",thing3.build());

            ParamsBuilder params = ParamsBuilder.newBuild()
                    .addParam("template_id", WxConstants.WORK_ENTRY_SUCCESS)
                    .addParam("data",data.build());

                params.addParam("touser", userInfoBO.getWxOpenId());
                String resData = HttpUtil.post(WxConstants.SEND_MESSAGE, JSON.toJSONString(params.build()));
                JSONObject param = JSON.parseObject(resData);
                logger.info(param.toString());



    }



    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        WorkEntryBO reqData = super.getData(sign, WorkEntryBO.class);

        if (Objects.isNull(reqData.getId())){
            throw new MallException( "入职申请id不能为空");
        }
        if (Objects.isNull(reqData.getIsEntry())){
            throw new MallException( "审核结果不能为空");
        }


        return null;
    }


    @Override
    public String getCmdCode() {
        return WorkEntryCmdCode.WORK_ENTRY_PATCH;
    }

    @Autowired
    private IWorkEntryService WorkEntryService;
}